﻿using System;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using LDSCore;
using LDSModel;
using LDSModel.Dal;

namespace LDSProject.Forms
{
    public partial class QueryForm : Form
    {
        readonly Guid _deviceId;
        FormState _formState;
        bool _showAlert;

        public QueryForm(Guid deviceId)
        {
            InitializeComponent();

            _deviceId = deviceId;
        }

        private void QueryForm_Load(object sender, EventArgs e)
        {
            cbOilTank.DataSource = DeviceService.Instance.OilTankList;
            cbOilTank.SelectedIndex = -1;

            _formState = FormState.Loading;

            var device = DeviceService.Instance.GetDevice(_deviceId);
            if (null != device)
            {
                var first = DeviceService.Instance.OilTankList.FirstOrDefault(d => d.OId == device.OID);
                if (first != null) cbOilTank.SelectedIndex = cbOilTank.Items.IndexOf(first);
            }

            _formState = FormState.Loaded;

            dtStartTime.MaxDate = DateTime.Now;
            dtEndTime.MaxDate = DateTime.Now;

            btnQuery.PerformClick();

            _showAlert = true;
        }

        private void cbOilTank_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (_formState == FormState.None) return;

            btnExcel.Enabled = false;

            var list = DeviceService.Instance.LoadDeviceList((Guid)cbOilTank.SelectedValue);
            cbDevice.DataSource = list;

            switch (_formState)
            {
                case FormState.Loading:
                    {
                        if (list != null)
                        {
                            var first = list.FirstOrDefault(d => d.DeviceId == _deviceId);
                            if (first != null) cbDevice.SelectedIndex = cbDevice.Items.IndexOf(first);
                        }
                    }
                    break;
                case FormState.Loaded:
                    {
                        cbDevice.SelectedIndex = -1;
                    }
                    break;
            }
        }

        private void btnQuery_Click(object sender, EventArgs e)
        {
            dgRecord.Rows.Clear();

            var dataList = RecordService.Query(
                (cbOilTank.SelectedValue ?? "").ToString(),
                (cbDevice.SelectedValue ?? "").ToString(),
                dtStartTime.Value.Date, dtEndTime.Value.Date);

            foreach (var item in dataList)
            {
                dgRecord.Rows.Add(item.OilName, item.DeviceName, item.AlarmCount, item.AlarmTime, item.AValue, item.BValue);
            }

            toolStripStatusLabel1.Text = string.Format("共查询到{0}条雷击记录.", dgRecord.Rows.Count);
            
            btnExcel.Enabled = true;

            if (dgRecord.Rows.Count != 0) return;
            if (!_showAlert) return;
            dgRecord.Rows.Add("", "", 0, DateTime.Now, 0, 0);
            MessageBox.Show(this, @"未查询到满足条件的记录!", @"提示");
        }

        private void btnExcel_Click(object sender, EventArgs e)
        {
            saveFileDialog1.FileName = string.Format("{0}{1}({2}-{3})雷击记录",
                (cbOilTank.SelectedItem as Data_OilTank ?? new Data_OilTank()).Name,
                (cbDevice.SelectedItem as Data_Device ?? new Data_Device()).Name,
                dtStartTime.Text,
                dtEndTime.Text);

            if (DialogResult.OK != saveFileDialog1.ShowDialog(this)) return;
            var dt = new DataTable {TableName = "test"};
            //添加列
            for (var i = 0; i < dgRecord.Columns.Count; i++)
            {
                dt.Columns.Add(dgRecord.Columns[i].HeaderText);
            }

            //添加行
            for (var j = 0; j < dgRecord.Rows.Count; j++)
            {
                var dr = dt.NewRow();
                for (var k = 0; k < dgRecord.Columns.Count; k++)
                {
                    dr[k] = dgRecord.Rows[j].Cells[k].Value;
                }
                dt.Rows.Add(dr);
            }

            if (!Utils.ExportExcel(dt, saveFileDialog1.FileName)) return;
            toolStripStatusLabel1.Text = string.Format("数据导出成功,导出路径:{0}", saveFileDialog1.FileName);
            MessageBox.Show(this, @"导出路径:" + saveFileDialog1.FileName, @"数据导出成功!");
        }

        private void cbDevice_SelectedIndexChanged(object sender, EventArgs e)
        {
            btnExcel.Enabled = false;
        }

        private void dtStartTime_ValueChanged(object sender, EventArgs e)
        {
            btnExcel.Enabled = false;
        }

        private void dtEndTime_ValueChanged(object sender, EventArgs e)
        {
            btnExcel.Enabled = false;
        }

        private void dgRecord_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            var b = new SolidBrush(dgRecord.RowHeadersDefaultCellStyle.ForeColor);
            e.Graphics.DrawString((e.RowIndex + 1).ToString(System.Globalization.CultureInfo.CurrentUICulture), dgRecord.DefaultCellStyle.Font, b, e.RowBounds.Location.X + 20, e.RowBounds.Location.Y + 4);
        }
    }
}
